package org.pi.common.plot.figures

class AwtFigure extends java.awt.Canvas with org.pi.common.plot.figures.BaseFigure {
  
  addObserver(new org.pi.common.observerpattern.Observer {
	override def notifiedByObservable(obs: org.pi.common.observerpattern.Observable) {
	  repaint	 
	}
  })
  
  override def paint(_graphics: java.awt.Graphics) {
	val graphics = _graphics.asInstanceOf[java.awt.Graphics2D]
	graphics.setClip(0,0,getWidth(),getHeight())
    paintFigure(graphics)
  }

//  override def update(g: java.awt.Graphics) {
//	  println("update")
//	  super.update(g)
//  }
	
	override def activate() {
		this.setVisible(true)
	}
}

object AwtFigure {
	
	def figure = {
		val fig = new AwtFigure()
	    val f = new java.awt.Frame();
		f.setLayout( new java.awt.BorderLayout() )
	    f.add(java.awt.BorderLayout.CENTER, fig)
	    f.addWindowListener(new java.awt.event.WindowAdapter{
	      override def windowClosing(event: java.awt.event.WindowEvent) {
	        f.dispose()
	      }
	    })
	    f.pack()
	    f.setSize(400,200)
	    f.setVisible(true)
		fig
	}
	
	def main(args: Array[String]) {
		print("Test AwtFigure")
		val fig = figure
		var axes : org.pi.common.plot.axes.Axes = null 
		var line : org.pi.common.plot.drawables.Line = null 
		for (j <- 0 until 2) {
		    axes = new org.pi.common.plot.axes.SimpleAxes()
		    axes.addDrawable( new org.pi.common.plot.drawables.Line(20,10,80,30) )
		    line = new org.pi.common.plot.drawables.Line(120,50,90,20) 
		    axes.addDrawable( line )
		    fig.subplot(2,1,j)
		    fig.setCurrentAxes(axes)
		}
		org.pi.common.snippets.SimpleAction("setLW") {
			val r = new java.util.Random()
			line.lineWidth = r.nextFloat()*5+1;
			line.color = new java.awt.Color(r.nextFloat(),r.nextFloat(),r.nextFloat())
		}
		org.pi.common.snippets.SimpleAction("save") {
			fig.saveAs("testfigure.png")
		}
		org.pi.common.snippets.SimpleAction("clear") {
			axes.clear
		}
		org.pi.common.snippets.SimpleAction("addLine") {
			val r = new java.util.Random()
		    line = new org.pi.common.plot.drawables.Line(r.nextFloat()*200,r.nextFloat()*200,r.nextFloat()*200,r.nextFloat()*200) 
		    axes.addDrawable( line )
		}
	}
}